package com.admin.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author Uni
 * @create 2021/12/2 18:55
 */
public class JDBCUtil {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    static {
        try {
            InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
            Properties p = new Properties();
            p.load(in);
            System.out.println(in);
            driver = p.getProperty("driver");
            url = p.getProperty("url");
            username = p.getProperty("username");
            password = p.getProperty("password");
            System.out.println("连接Mysql: " + driver + url);
            // 加载驱动
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    public static Connection getConnect(){
        Connection con = null;
        try {
            con = DriverManager.getConnection(url, username, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return con;
    }
    public static void release(Connection con, Statement st, ResultSet rs) throws SQLException {
        if (rs != null) rs.close();
        if (st != null) st.close();
        if(con!=null) con.close();
    }
}
